<template>
  <div class="zj">
      <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>{{ userinfo.nickname }}</span>
        <el-button class="button" text><a href="/about">返回用户页面</a></el-button>
      </div>
    </template>
    <div class="tp">
      <img :src="userinfo.TbUserInfo.img" alt=""    style="width: 50%; height: 50%;">
    </div>
    <div class="text item">用户昵称: {{ userinfo.TbUserInfo.nickname }}</div>
    <div class="text item">用户性别：{{ userinfo.TbUserInfo.sex }}</div>
    <div class="text item">用户简介：{{ userinfo.TbUserInfo.info }}</div>
    <div class="text item">邮箱账号：{{ userinfo.TbUserInfo.email }}</div>
    <div class="text item">手机号码：{{ userinfo.TbUserInfo.phone }}</div>
    <div class="text item">qq号码: {{ userinfo.TbUserInfo.qq }}</div>
    <div class="text item">微信账号：{{ userinfo.TbUserInfo.wechat }}</div>
    <div class="text item">用户信息修改时间：{{ Tools.formatDate(userinfo.TbUserInfo.lastupdate) }}</div>
    <hr />
    <el-button link type="primary" size="small" @click="updates()">修改个人信息</el-button>
  </el-card>
  </div>


  <ElDialog v-model="dialogurl" title="选择历史图片为头像" width="30%" :before-close="handleClose">
    <HistoryComp />
    <el-button @click="dialogVisible = false,dialogurl=false,userquery()" >关闭</el-button>
  </ElDialog>

  <!-- 修改用户信息对话框部分 -->
  <el-dialog v-model="dialogVisible" title="用户信息修改" width="30%" :before-close="handleClose">
    <div style="margin: 20px" />

    <el-form :label-position="labelPosition" label-width="100px" :model="updateinfo" style="max-width: 460px">
      <ElButton @click="dialogurl = true">
        历史图片
      </ElButton>
      <el-form-item label="图片：">
        <FileComp />
      </el-form-item>
      <el-form-item label="用户姓名：">
        <el-input v-model="updateinfo.nickname" placeholder="用户昵称" clearable label-width="100px" />
      </el-form-item>
      <el-form-item label="用户性别：">
        <el-radio-group v-model="updateinfo.sex">
          <el-radio label="m" />
          <el-radio label="f" />
          <el-radio label="n" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户简介：">
        <el-input v-model="updateinfo.info" placeholder="用户简介" clearable />
      </el-form-item>
      <el-form-item label="邮箱账号：">
        <el-input v-model="updateinfo.email" placeholder="邮箱账号" clearable />
      </el-form-item>
      <el-form-item label="手机号码：">
        <el-input v-model="updateinfo.phone" placeholder="手机号码" clearable />
      </el-form-item>
      <el-form-item label="qq号码： ">
        <el-input v-model="updateinfo.qq" placeholder="qq号码" clearable />
      </el-form-item>
      <el-form-item label="微信账号：">
        <el-input v-model="updateinfo.wechat" placeholder="微信账号" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="update"> 修改 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElCard, ElButton, ElDialog, ElFormItem, ElForm, ElInput, ElRadioGroup, ElRadio, ElMessageBox } from 'element-plus'
import { L12030 } from '../../api/L12030'
import { BaseResult } from '../../ts/BaseResult'
import { ref } from 'vue'
import { UserInfo, TbUserInfo } from '../../ts/UserInfo'
import { Tools } from '../../ts/Tools'
import FileComp from '../../components/FileComp.vue'
import HistoryComp from '../../components/HistoryComp.vue'
const labelPosition = ref<FormProps['labelPosition']>('right')
const userinfo = ref(new UserInfo())
const dialogVisible = ref(false)
const dialogurl = ref(false)
const updateinfo = ref(new TbUserInfo())
function userquery() {
  L12030.get('/auth', {}, (data: BaseResult) => {
    if (data.success) {
      userinfo.value.TbUserInfo = data.data.tbUserInfo
      if (userinfo.value.TbUserInfo.sex == 'n') {
        userinfo.value.TbUserInfo.sex = '保密'
      }
      if (userinfo.value.TbUserInfo.sex == 'm') {
        userinfo.value.TbUserInfo.sex = '男'
      }
      if (userinfo.value.TbUserInfo.sex == 'f') {
        userinfo.value.TbUserInfo.sex = '女'
      }
      userinfo.value.TbUser = data.data.tbUser
      userinfo.value.TbRole = data.data.tbRole
    }
  })
}



function updates() {
  updateinfo.value = Tools.concatJson(userinfo.value.TbUserInfo)
  dialogVisible.value = true
}

function update() {
  L12030.put('/userinfo', updateinfo.value, (data: BaseResult) => {
    data.success ? ElMessageBox.alert('修改成功') : ElMessageBox.alert('修改失败')
    dialogVisible.value=false
    userquery()
  })
}
userquery()
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}
.zj{
  position: fixed;  
  top: 50%;  
  left: 50%;  
  transform: translate(-50%, -50%);
  opacity: 0.7; 
}
.item {
  margin-bottom: 18px;
}
.tpdx {
  width: 30px;
  height: 30px;
}
.box-card {
  width: 480px;
  margin: 0rem 5rem;
}
.tp{
  width: 300px;  
  height: auto;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
